package dao;

import entity.Billet;
import entity.Evenement;
import entity.TypeBillet;
import entity.Typeb;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.print.PrintException;

public class NewMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        Typeb typebGradin = new TypebDAO().findById(14);
        Typeb typebChaise = new TypebDAO().findById(32);
        System.out.println("Type :" + typebGradin.getId() + " prix:" + typebGradin.getPrix());
        System.out.println("Type :" + typebChaise.getId() + " prix:" + typebChaise.getPrix());
        Billet b = new Billet();
        BilletDAO BilletDAO = new BilletDAO();
        BilletDAO.beginTransaction();
        Writer writer = null;
//        for (int i = 0; i < 31; i++) {

         //   Evenement lists = new EvenementDAO().findById(i + 1);
            //String ss = lists.getTitre();
//            ss = ss.replaceAll("'", "_");
//            ss = lists.getTitre().replaceAll(" ", "_");
//            System.out.println("Spec=" + ss);
//            if (ss.length() > 19) {
//                ss = ss.substring(0, 19);
//            }
            File file = new File("e:\\CC\\Pass\\Staff15.csv");


            writer = new BufferedWriter(new FileWriter(file));
            typebGradin = new TypebDAO().findById(335);

            for (int j = 311; j <= 325; j++) {

//                String s = ajoutzRandomEvt((lists.getId()) + "", 4)
//                        + "" + ajoutzRandomType(typebGradin.getId() + "", 4) + ""
//                        + "" + inverserBillet(remplirCase(j))
//                        + "-" + lists.getTitre().substring(0, 2).toUpperCase() + ""
//                        + "" + day(lists.getDate());

                String s=ajoutzRandomEvt((4) + "", 4)
                       + "" + ajoutzRandomType(typebGradin.getId() + "", 4) + ""
                        + "" + inverserBillet(remplirCase(j))
                        + "-SO55";
                writer.write(s + ";;\n");
                b.setNumero(s);
                b.setUtilise(false);
                b.setVendu(false);
                b.setBnrAcces(0);
                b.setTypeb(typebGradin);
                BilletDAO.save(b);
                b = new Billet();
                System.out.print(s);
                System.out.println("    ID Event:" + s.substring(1, 3)
                        + "    ID Type ticket:" + s.substring(4, 6)
                        + "  j=" + Integer.parseInt(inverserBillet(s.substring(8, 12))));
            }
            writer.close();
//        }
        System.out.println("Before ");
        BilletDAO.commit();
        System.out.println("After ");
    }

    public static String day(Date d) {
        return new SimpleDateFormat("dd").format(d);
    }

    public static String ajoutzRandomEvt(String s, int n) {
        int pref = new Random().nextInt(10);
        int suf = new Random().nextInt(10);
        if (s.length() != 2) {
            s = "0" + s;
        }
        return pref + s + suf;
    }

    public static String ajoutzRandomType(String s, int n) {
        int pref = new Random().nextInt(10);
        int suf = new Random().nextInt(10);
        if (s.length() != 2) {
            s = "0" + s;
        }
        return s + pref + "" + suf;
    }

    public static String inverserBillet(String s) {
        String inv = "";
        for (int i = 0; i < s.length(); i++) {
            inv = s.charAt(i) + inv;
        }
        return inv;
    }

    public static String remplirCase(int n) {
        String s = String.valueOf(n);
        String inv = s;
        for (int i = 0; i < 4 - s.length(); i++) {
            inv = "0" + inv;
        }
        return inv;
    }

    public static String ajoutz(String s, int n) {
        String ss = "";


        for (int i = 0; i < n - s.length(); i++) {
            ss += "0";
        }
        return ss + s;
    }
}
